package com.qf.homeproject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class NewDatailsActivity extends Activity
{

	private String detailUrl;
	RequestQueue queue;
	List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
	List<ImageView> imgData = new ArrayList<ImageView>();
	private TextView titleView;
	private TextView infoView;
	private WebView webView;
	private String title;
	private String info;
	private String data;
	AnimationDrawable anim;
	boolean isLoading = true;

	private ImageView imageView;
	private ScrollView scrollView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_new_datails);
		initView();
		initUrl();
		loadDataFromServer(detailUrl);
		// initLoadImg();
		initAnimation();
	}

	/**
	 * 初始化详情页面的动画
	 */
	private void initAnimation()
	{
		imageView.setBackgroundResource(R.anim.frame_anim);
		anim = (AnimationDrawable) imageView.getBackground();
		if (!anim.isRunning())
		{
			// 开始逐帧动画
			anim.start();
		}
	}

	private void initLoadImg()
	{
		ImageView imgView = null;
		TypedArray imgArray = getResources()
				.obtainTypedArray(R.array.imgarrays);
		for (int i = 0; i < imgArray.length(); i++)
		{
			imageView.setScaleType(ScaleType.CENTER_CROP);
			imageView.setImageDrawable(imgArray.getDrawable(i));

		}
	}

	private void initData()
	{

		Log.i("info", "集合的大小为:" + datas.size());
		title = datas.get(0).get("title");
		String source = datas.get(0).get("source");
		String time = datas.get(0).get("time");
		data = datas.get(0).get("data");
		info = source + "  " + time;

		Log.i("info", "新闻的标题为" + data);
		// 给TextView设置数值
		titleView.setText(title);
		infoView.setText(info);

		// webView加载数据
		webView.loadData(data, "text/html;charset=utf-8", "utf-8");

	}

	private void initView()
	{
		titleView = (TextView) findViewById(R.id.new_title_id);
		infoView = (TextView) findViewById(R.id.infoId);
		webView = (WebView) findViewById(R.id.web_view_id);
		imageView = (ImageView) findViewById(R.id.img_view_id);
		scrollView = (ScrollView) findViewById(R.id.scroll_view_id);
		// 设置布局的显示和隐藏
		scrollView.setVisibility(View.INVISIBLE);
		imageView.setVisibility(View.VISIBLE);
		webView.setVisibility(View.INVISIBLE);
	}

	private void initUrl()
	{
		queue = Volley.newRequestQueue(this);
		detailUrl = getIntent().getStringExtra("newDetailUrl");
		// Log.i("info", "详情页面的url为:" + detailUrl);
	}

	private void loadDataFromServer(String url)
	{
		isLoading = true;
		Log.i("info", "加载网络资源的url地址为" + url);
		StringRequest request = new StringRequest(url, new Listener<String>()
		{

			@Override
			public void onResponse(String response)
			{
				// Log.i("info", "详情页面下载下来的数据为:" + response);
				parseJson(response);
				Log.i("info", "详情页面解析完成的数据为:" + response);
				initData();
				// 数据加载完成
				isLoading = false;

				// 当网络数据下载完成之后，显示图片的ImageView隐藏,
				if (!isLoading)
				{
					anim.stop(); // 逐帧动画停止
					imageView.setVisibility(View.INVISIBLE);
					scrollView.setVisibility(View.VISIBLE);
					webView.setVisibility(View.VISIBLE);
				}
			}
		}, new ErrorListener()
		{

			@Override
			public void onErrorResponse(VolleyError error)
			{
				// TODO Auto-generated method stub

			}
		});
		queue.add(request);
	}

	public void parseJson(String json)
	{
		Map<String, String> map;
		try
		{
			JSONObject object = new JSONObject(json);
			String source = object.getString("source");
			String time = object.getString("time");
			String title = object.getString("title");
			JSONArray array_content = object.getJSONArray("content");
			for (int i = 0; i < array_content.length(); i++)
			{
				map = new HashMap<String, String>();
				JSONObject jsonObject = array_content.getJSONObject(i);
				if (i == 1)
				{
					String data = jsonObject.getString("value");
					map.put("source", source);
					map.put("time", time);
					map.put("title", title);
					map.put("data", data);
					datas.add(map);
				}
			}

			// initData();

			Log.i("info", "集合的大小为:" + datas.size());

		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void goBack(View view)
	{
		finish();
	}

	public void share(View view)
	{
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/*");
		intent.putExtra(Intent.EXTRA_TEXT, "给你分享一份不错的内容");
		startActivity(intent);
	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		if (queue != null)
		{
			queue.stop();
		}
	}
}
